cmake_minimum_required(VERSION 3.27)
project(MIPI_LVGL_DEMO)

set(TARGETS "MIPI_LVGL_DEMO")

#指定交叉编译环境
# set(CMAKE_SYSTEM_NAME Linux)
# set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)

# set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
# set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

#头文件路径
include_directories(
	.

	#动态库
	./lib/lib_custom_drv_db/include

	#驱动层
	./drv
	./drv/drv_mipi_lcd
	./drv/drv_sys_time

	#通用层
	./common

	#UI
	#UI 驱动
	./Middlewares/LVGL/GUI/
	./Middlewares/LVGL/GUI/lvgl
	./Middlewares/LVGL/GUI/lvgl/examples/porting
	./Middlewares/LVGL/GUI/lvgl/src
	./Middlewares/LVGL/GUI/lvgl/src/core
	./Middlewares/LVGL/GUI/lvgl/src/draw
	./Middlewares/LVGL/GUI/lvgl/src/draw/sw
	./Middlewares/LVGL/GUI/lvgl/src/draw/sdl
	./Middlewares/LVGL/GUI/lvgl/src/extra
	./Middlewares/LVGL/GUI/lvgl/src/extra/layouts
	./Middlewares/LVGL/GUI/lvgl/src/extra/layouts/flex
	./Middlewares/LVGL/GUI/lvgl/src/extra/layouts/grid
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/bmp
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/ffmpeg
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/freetype
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/fsdrv
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/gif
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/png
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/qrcode
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/rlottie
	./Middlewares/LVGL/GUI/lvgl/src/extra/libs/sjpg
	./Middlewares/LVGL/GUI/lvgl/src/extra/others/gridnav
	./Middlewares/LVGL/GUI/lvgl/src/extra/others/monkey
	./Middlewares/LVGL/GUI/lvgl/src/extra/others/snapshot
	./Middlewares/LVGL/GUI/lvgl/src/extra/themes
	./Middlewares/LVGL/GUI/lvgl/src/extra/themes/basic
	./Middlewares/LVGL/GUI/lvgl/src/extra/themes/default
	./Middlewares/LVGL/GUI/lvgl/src/extra/themes/mono
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/animimg
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/calendar
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/chart
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/colorwheel
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/imgbtn
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/keyboard
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/led
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/list
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/menu
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/meter
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/msgbox
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/span
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/spinbox
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/spinner
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/tabview
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/tileview
	./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/win
	./Middlewares/LVGL/GUI/lvgl/src/font
	./Middlewares/LVGL/GUI/lvgl/src/gpu
	./Middlewares/LVGL/GUI/lvgl/src/gpu
	./Middlewares/LVGL/GUI/lvgl/src/hal
	./Middlewares/LVGL/GUI/lvgl/src/misc
	./Middlewares/LVGL/GUI/lvgl/src/widgets
	#UI 应用
	./Middlewares/LVGL/GUI_APP/ui

	#应用层
	#应用层 UI
	./app/app_gui/ui_event_cb
	./app/app_gui/ui_main
	./app/app_gui/ui_sysinfo
	./app/app_gui/ui_sensordata
	./app/app_gui
	#应用层 功能
	./app/app_sensor_data
)

#添加源文件路径
aux_source_directory(.										DIR_SRCS)

#驱动层
aux_source_directory(./drv								DIR_DRV)
aux_source_directory(./drv/drv_mipi_lcd		DIR_DRV_MIPI_LCD)
aux_source_directory(./drv/drv_sys_time		DIR_DRV_SYS_TIME)

#通用层
aux_source_directory(./common							DIR_COMMON)

#UI
#UI 驱动
aux_source_directory(./Middlewares/LVGL/GUI/lvgl	DIR_LVGL)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/examples/porting	DIR_LVGL_PORTING)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src	DIR_LVGL_SRC)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/core	DIR_LVGL_SRC_CORE)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/draw	DIR_LVGL_SRC_DRAW)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/draw/sw	DIR_LVGL_SRC_DRAW_SW)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/draw/sdl	DIR_LVGL_SRC_DRAW_SDL)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra	DIR_LVGL_SRC_EXTRA)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/layouts	DIR_LVGL_SRC_EXTRA_LAYOUTS)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/layouts/flex	DIR_LVGL_SRC_EXTRA_LAYOUTS_FLEX)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/layouts/grid	DIR_LVGL_SRC_EXTRA_LAYOUTS_GRID)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs	DIR_LVGL_SRC_EXTRA_LIBS)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/bmp	DIR_LVGL_SRC_EXTRA_LIBS_BMP)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/ffmpeg	DIR_LVGL_SRC_EXTRA_LIBS_FFMPEG)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/freetype	DIR_LVGL_SRC_EXTRA_LIBS_FREETYPE)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/fsdrv	DIR_LVGL_SRC_EXTRA_LIBS_FSDRV)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/gif	DIR_LVGL_SRC_EXTRA_LIBS_GIF)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/png	DIR_LVGL_SRC_EXTRA_LIBS_PNG)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/qrcode	DIR_LVGL_SRC_EXTRA_LIBS_QRCODE)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/rlottie	DIR_LVGL_SRC_EXTRA_LIBS_RLOTTIE)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/libs/sjpg	DIR_LVGL_SRC_EXTRA_LIBS_SJPG)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/others	DIR_LVGL_SRC_EXTRA_OTHERS)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/others/gridnav	DIR_LVGL_SRC_EXTRA_OTHERS_GRIDNAV)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/others/monkey	DIR_LVGL_SRC_EXTRA_OTHERS_MONKEY)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/others/snapshot	DIR_LVGL_SRC_EXTRA_OTHERS_snapshot)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/themes	DIR_LVGL_SRC_EXTRA_THEMES)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/themes/basic	DIR_LVGL_SRC_EXTRA_THEMES_BASIC)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/themes/default	DIR_LVGL_SRC_EXTRA_THEMES_DEFAULT)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/themes/mono	DIR_LVGL_SRC_EXTRA_THEMES_MONO)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets	DIR_LVGL_SRC_EXTRA_WIDGETS)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/animimg	DIR_LVGL_SRC_EXTRA_WIDGETS_ANIMIMG)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/calendar	DIR_LVGL_SRC_EXTRA_WIDGETS_CALENDAR)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/chart	DIR_LVGL_SRC_EXTRA_WIDGETS_CHART)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/colorwheel	DIR_LVGL_SRC_EXTRA_WIDGETS_COLORWHEEL)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/imgbtn	DIR_LVGL_SRC_EXTRA_WIDGETS_IMGBTN)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/keyboard	DIR_LVGL_SRC_EXTRA_WIDGETS_KEYBOARD)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/led	DIR_LVGL_SRC_EXTRA_WIDGETS_LED)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/list	DIR_LVGL_SRC_EXTRA_WIDGETS_LSIT)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/menu	DIR_LVGL_SRC_EXTRA_WIDGETS_MENU)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/meter	DIR_LVGL_SRC_EXTRA_WIDGETS_METER)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/msgbox	DIR_LVGL_SRC_EXTRA_WIDGETS_MSGBOX)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/span	DIR_LVGL_SRC_EXTRA_WIDGETS_SPAN)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/spinbox	DIR_LVGL_SRC_EXTRA_WIDGETS_SPINBOX)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/spinner	DIR_LVGL_SRC_EXTRA_WIDGETS_SPINNER)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/tabview	DIR_LVGL_SRC_EXTRA_WIDGETS_TABVIEW)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/tileview	DIR_LVGL_SRC_EXTRA_WIDGETS_TILEVIEW)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/extra/widgets/win	DIR_LVGL_SRC_EXTRA_WIDGETS_WIN)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/font	DIR_LVGL_SRC_FONT)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/gpu	DIR_LVGL_SRC_GPU)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/gpu	DIR_LVGL_SRC_GPU)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/hal	DIR_LVGL_SRC_HAL)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/misc	DIR_LVGL_SRC_MISC)
aux_source_directory(./Middlewares/LVGL/GUI/lvgl/src/widgets	DIR_LVGL_SRC_WIDGETS)

#应用层
#应用层 UI
aux_source_directory(./app/app_gui/ui_event_cb		DIR_APP_GUI_UI_EVENT_CB)
aux_source_directory(./app/app_gui/ui_main				DIR_APP_GUI_UI_MAIN)
aux_source_directory(./app/app_gui/ui_sysinfo			DIR_APP_GUI_UI_SYSINFO)
aux_source_directory(./app/app_gui/ui_sensordata	DIR_APP_GUI_UI_SENSORDATA)
aux_source_directory(./app/app_gui								DIR_APP_GUI)
#应用层 功能
aux_source_directory(./app/app_sensor_data				DIR_APP_SENSOR_DATA)

#添加动态连接库的路径
link_directories(./lib)
#数据库驱动
link_directories(./lib/lib_custom_drv_db/lib)
#############
set(MIN_VERSION "1.0.0")
# 动态库目录
set(LIB_DIR "./lib/lib_custom_drv_db/lib")

# 步骤 1：直接查找符合版本要求的动态库文件（例如 libcustom_drv_db.so.1.2.3）
find_library(CUSTOM_DRV_DB_LIB
	NAMES "libcustom_drv_db.so.${MIN_VERSION}"		# 匹配 = MIN_VERSION 的库
	PATHS ${LIB_DIR}
	NO_DEFAULT_PATH
)

# 步骤 2：如果未找到，尝试查找更高版本
if (NOT CUSTOM_DRV_DB_LIB)
	# 列出所有库文件
	file(GLOB LIB_FILES "${LIB_DIR}/libcustom_drv_db.so.*")
	# 遍历所有文件名
	foreach (LIB IN LISTS LIB_FILES)
		# REGEX MATCH 使用正则 从 文件名中 提取版本号（例如 1.2.3）
		string(REGEX MATCH "([0-9]+\\.[0-9]+\\.[0-9]+)$" VERSION_MATCH ${LIB})
		#如果找到CMAKE_MATCH_1存在
		#CMAKE_MATCH_1存在 && 按数值比较版本号 大于 MIN_VERSION
		if (CMAKE_MATCH_1 AND CMAKE_MATCH_1 VERSION_GREATER_EQUAL MIN_VERSION)
			#设置版本号
			set(CUSTOM_DRV_DB_LIB ${LIB})
			break()
		endif ()
	endforeach ()
endif ()

# 步骤 3：验证是否找到符合条件的库
if (CUSTOM_DRV_DB_LIB)
	message(STATUS "找到动态库：${CUSTOM_DRV_DB_LIB}")
else ()
	message(FATAL_ERROR "未找到版本 ${MIN_VERSION} 的动态库！")
endif ()
#############

#添加UI子目录
add_subdirectory(./Middlewares/LVGL/GUI_APP/ui)

#添加生成目标
add_executable(
		${TARGETS}	${DIR_SRCS}
		
								#驱动层
								${DIR_DRV}
								${DIR_DRV_MIPI_LCD}
								${DIR_DRV_SYS_TIME}
								${DIR_DRV_DB}

								#通用层
								${DIR_COMMON}

								#UI
								#UI 驱动
								${DIR_LVGL}
								${DIR_LVGL_PORTING}
								${DIR_LVGL_SRC}
								${DIR_LVGL_SRC_CORE}
								${DIR_LVGL_SRC_DRAW}
								${DIR_LVGL_SRC_DRAW_SW}
								${DIR_LVGL_SRC_DRAW_SDL}
								${DIR_LVGL_SRC_EXTRA}
								${DIR_LVGL_SRC_EXTRA_LAYOUTS}
								${DIR_LVGL_SRC_EXTRA_LAYOUTS_FLEX}
								${DIR_LVGL_SRC_EXTRA_LAYOUTS_GRID}
								${DIR_LVGL_SRC_EXTRA_LIBS}
								${DIR_LVGL_SRC_EXTRA_LIBS_BMP}
								${DIR_LVGL_SRC_EXTRA_LIBS_FFMPEG}
								${DIR_LVGL_SRC_EXTRA_LIBS_FREETYPE}
								${DIR_LVGL_SRC_EXTRA_LIBS_FSDRV}
								${DIR_LVGL_SRC_EXTRA_LIBS_GIF}
								${DIR_LVGL_SRC_EXTRA_LIBS_PNG}
								${DIR_LVGL_SRC_EXTRA_LIBS_QRCODE}
								${DIR_LVGL_SRC_EXTRA_LIBS_RLOTTIE}
								${DIR_LVGL_SRC_EXTRA_LIBS_SJPG}
								${DIR_LVGL_SRC_EXTRA_OTHERS}
								${DIR_LVGL_SRC_EXTRA_OTHERS_GRIDNAV}
								${DIR_LVGL_SRC_EXTRA_OTHERS_MONKEY}
								${DIR_LVGL_SRC_EXTRA_OTHERS_snapshot}
								${DIR_LVGL_SRC_EXTRA_THEMES}
								${DIR_LVGL_SRC_EXTRA_THEMES_BASIC}
								${DIR_LVGL_SRC_EXTRA_THEMES_DEFAULT}
								${DIR_LVGL_SRC_EXTRA_THEMES_MONO}
								${DIR_LVGL_SRC_EXTRA_WIDGETS}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_ANIMIMG}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_CALENDAR}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_CHART}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_COLORWHEEL}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_IMGBTN}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_KEYBOARD}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_LED}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_LSIT}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_MENU}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_METER}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_MSGBOX}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_SPAN}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_SPINBOX}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_SPINNER}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_TABVIEW}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_TILEVIEW}
								${DIR_LVGL_SRC_EXTRA_WIDGETS_WIN}
								${DIR_LVGL_SRC_FONT}
								${DIR_LVGL_SRC_GPU}
								${DIR_LVGL_SRC_GPU}
								${DIR_LVGL_SRC_HAL}
								${DIR_LVGL_SRC_MISC}
								${DIR_LVGL_SRC_WIDGETS}

								#应用层
								#应用层 UI
								${DIR_APP_GUI_UI_EVENT_CB}
								${DIR_APP_GUI_UI_MAIN}
								${DIR_APP_GUI_UI_SYSINFO}
								${DIR_APP_GUI_UI_SENSORDATA}
								${DIR_APP_GUI}
								#应用层 功能
								${DIR_APP_SENSOR_DATA}
)

target_link_libraries(
	${TARGETS} gpiod pthread evdev ui custom_drv_db
)


